// mircoApp/local_life/store/index/index.js
var app = getApp();
const skinBehavior = require('../../../../utils/themeSkin/skinBehavior');
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    is_has_pic: false,
    comment_list: [],
    latitude: "",
    longitude: "",
    id: "",
    info: {},
    list: [],
    goods_list: [],
    page: 1,
    total_page: 1,
    point: {
      lat: "",
      lng: ""
    },
    language: "",
    photo_list: [],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en
    })
    this.data.id = options.id || '';
    let address_point = wx.getStorageSync("address-point");
    if (address_point) {
      this.data.point.lng = address_point.point.lng;
      this.data.point.lat = address_point.point.lat;
    }

    this.getStore();
    this.getGoods();
    this.getComment();
    this.get_like_goods()
  },
  jumpDetails(e) {
    let item = e.currentTarget.dataset.item;
    let id =  item?.has_many_goods_relation[0]?.goods_id;
    wx.navigateTo({
      url: '/mircoApp/local_life/goods/goods?goods_id=' + id
    });
  },
  previewImg(e) {
    let ind;
    ind = e.currentTarget.dataset.key;
    wx.previewImage({
      current: ind ? this.data.photo_list[ind] : this.data.photo_list, // 当前显示图片的http链接
      urls: this.data.photo_list // 需要预览的图片http链接列表
    });
  },
  format(start_time) {
    var start_date = new Date(start_time * 1);
    var start_hour = start_date.getHours() < 10 ? '0' + start_date.getHours() : start_date.getHours();
    var start_minute = start_date.getMinutes() < 10 ? (0 + (start_date.getMinutes() + '')) : start_date.getMinutes();
    return `${start_hour}:${start_minute}`;
  },
  goto_comment() {
    wx.navigateTo({
      url: '/mircoApp/local_life/comment/comment?store_id=' + this.data.id
    })
  },
  get_like_goods(page = 1) {
    let json = {
      page
    };
    if (this.data.point.lng) {
      json.latitude = this.data.point.lat * 1;;
      json.longitude = this.data.point.lng * 1;;
    }

    // let local_life_code = wx.getStorageSync('local_life_code');
    // if (local_life_code) {
    //   let city_id = local_life_code+'';
    //   let _city = city_id.slice(0,-2) + '00';//市的city_id
    //   json.city_id = _city
    // }
    app._postNetWork({
      url: app.getNetAddresss("plugin.local-life.api.store.nearest-store"),
      data: json,
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result) {
          if (data&&data.data&&data.data.length !== 0) {
            let goods_list = page == 1 ? data.data : this.data.goods_list.concat(data.data);
            this.setData({
              goods_list
            });
            this.data.page = data.current_page;
            this.data.total_page = data.last_page;
          }

        } else {
          app.tips(msg);
        }
      }
    })
  },
  getStore() {
    let json = {
      id: this.data.id * 1
    };
    if (this.data.point.lng) {
      json.latitude = this.data.point.lat * 1;;
      json.longitude = this.data.point.lng * 1;;
    }

    app._postNetWork({
      url: app.getNetAddresss("plugin.local-life.api.store.info"),
      data: json,
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result && data) {
          wx.setNavigationBarTitle({
            title: data.name,
          })
          data.open_time.forEach(item => {
            item.times.forEach(c_item => {
              c_item.start_time_txt = this.format(c_item.start_time);
              c_item.end_time_txt = this.format(c_item.end_time);
            })
          })
          let photo_list = [];
          if (data.has_one_brand?.brand_pic_tag_list && data.has_one_brand?.brand_pic_tag_list.length !== 0) {
            data.has_one_brand?.brand_pic_tag_list.forEach(item => {
              if (item.pics.lenth !== 0) {
                item.pics.forEach(c_item => {
                  if (c_item.is_recommend) {
                    photo_list.push(c_item.image_url);
                  }
                })
              }
            })
          }

          if (data.has_one_brand?.brand_pic_list.length !== 0) {
            data.has_one_brand?.brand_pic_list.forEach(item => {
              if (item.is_recommend) {
                photo_list.push(item.image_url);
              }
            })
          }

          this.setData({
            photo_list,
            info: data,
          })
        } else {
          app.tips(msg);
        }
      }
    })
  },
  goto_discounts() {
    wx.navigateTo({
      url: '/mircoApp/local_life/store/discounts/discounts?store_id=' + this.data.id
    })
  },
  getGoods() {
    app._postNetWork({
      url: app.getNetAddresss("plugin.local-life.api.store.relate-goods"),
      data: {
        id: this.data.id * 1
      },
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result) {
          this.setData({
            list: data.data
          });
        } else {
          app.tips(msg);
        }
      }
    })
  },
  getComment() {
    app._postNetWork({
      url: app.getNetAddresss("plugin.local-life.api.store.comment-list"),
      data: {
        store_id: this.data.id * 1
      },
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result) {
          let comment_list = (data.data.length !== 0 && data.data.splice(0, 2)) || []
          this.setData({
            comment_list
          });
        } else {
          app.tips(msg);
        }
      }
    })
  },
  goto(e) {
    let type = e.currentTarget.dataset.url;
    if (type == "home") {
      wx.navigateTo({
        url: '/mircoApp/local_life/index/index',
      })
    } else if (type == "order") {

      wx.navigateTo({
        url: '/packageA/member/myOrder_v2/myOrder_v2?status=0&orderType=local-life',
      })
    } else if (type == 'photo') {
      wx.navigateTo({
        url: '/mircoApp/local_life/store/photo/photo?store_id=' + this.data.id,
      })
    } else if (type == 'detail') {
      wx.navigateTo({
        url: '/mircoApp/local_life/store/details/details?id=' + this.data.id,
      })
    } else {
      wx.navigateTo({
        url: '/mircoApp/local_life/publish_comment/publish_comment?id=' + this.data.id,
      })
    }
  },
  navigation() { //查看商家的地址定位信息
    let latitude = this.data.info.latitude;
    let longitude = this.data.info.longitude;
    let point = app.BMapTransqqMap(parseFloat(longitude), parseFloat(latitude));
    wx.openLocation({
      latitude: point.lat,
      longitude: point.lng,
      name: this.data.info.name,
      scale: 18
    });
  },
  call_phone() {
    let number = this.data.info.tel;
    wx.makePhoneCall({
      phoneNumber: number,
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

    if (this.data.page >= this.data.total_page) return;
    let page = this.data.page + 1;
    this.get_like_goods(page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});